package net.sureon.web;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import net.sureon.common.SureonConstants;

public class AppCookie {
	private String token;
	private String siteURL;
	private long userID;
	private String userName;
	private Cookie tokenCookie;
	private Cookie userIDCookie;
	
	public AppCookie(Cookie[] cookies) {
		if(cookies != null){
			for (Cookie cookie : cookies) {
				if (SureonConstants.COOKIE_TOKEN.equals(cookie.getName())) {
					token = cookie.getValue();
					tokenCookie = cookie;
				}
				if (SureonConstants.COOKIE_SITEURL.equals(cookie.getName())) {
					siteURL = cookie.getValue();
					userIDCookie = cookie;
				}
				if (SureonConstants.COOKIE_USERID.equals(cookie.getName())) {
					userID = Long.parseLong(cookie.getValue());
				}
				if (SureonConstants.COOKIE_USERNAME.equals(cookie.getName())) {
					userName = cookie.getValue();
				}				
			}
		}
	}

	/**
	 * @return the token
	 */
	public String getToken() {
		return token;
	}

	/**
	 * @param token
	 *            the token to set
	 */
	public void setToken(String token) {
		this.token = token;
	}

	/**
	 * @return the siteURL
	 */
	public String getSiteURL() {
		return siteURL;
	}

	/**
	 * @param siteURL
	 *            the siteURL to set
	 */
	public void setSiteURL(String siteURL) {
		this.siteURL = siteURL;
	}

	/**
	 * @return the userID
	 */
	public long getUserID() {
		return userID;
	}

	/**
	 * @param userID
	 *            the userID to set
	 */
	public void setUserID(long userID) {
		this.userID = userID;
	}
	
	
	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public void saveUserName(HttpServletResponse response){
		addCookie(response, SureonConstants.COOKIE_USERNAME, userName);
	}
	
	public void saveSiteURL(HttpServletResponse response){
		addCookie(response, SureonConstants.COOKIE_SITEURL, siteURL);
	}

	public void saveToken(HttpServletResponse response){
		addCookie(response, SureonConstants.COOKIE_TOKEN, token);
		addCookie(response, SureonConstants.COOKIE_USERID, String.valueOf(userID));
	}
	
	public void resetToken(HttpServletResponse response){
		setToken(null);
		setUserID(0);
		if(tokenCookie != null){
			tokenCookie.setValue("");
			tokenCookie.setMaxAge(0);
		}
		if(userIDCookie != null){
			userIDCookie.setValue("");
			userIDCookie.setMaxAge(0);
		}
		delCookie(response, SureonConstants.COOKIE_TOKEN);
		delCookie(response, SureonConstants.COOKIE_USERID);
	}	
	
	public static void addCookie(HttpServletResponse response, String name, String value){
		if(name != null && value != null){
			Cookie cookie = new Cookie(name, value);
			cookie.setMaxAge(SureonConstants.COOKIE_MAX_AGE);
			response.addCookie(cookie);
		}
	}
	
	public static void delCookie(HttpServletResponse response, String name){
		if(name != null){
			Cookie cookie = new Cookie(name, "");
//			cookie.setPath("/");
			cookie.setMaxAge(0);
			response.addCookie(cookie);
		}
	}

}
